
export interface IRouter {
  path: string; 
  name: string; 
  component: () => Promise<any> | any, 
  children?: IRouter[] 
}

export interface IMenu {
  path: string; 
  name: string;
  title: string;
  icon: any,
  component: () => Promise<any> | any, 
  isMenu: boolean,
  isRouter: boolean,
  children?: IMenu[] 
}

 
/**
 * 把配置转换为路由
 */
export const toRouter = (menus: IMenu[]) => {
  // 路由集合
  const re: IRouter[] = []
  // 遍历配置
  menus.forEach((menu: any) => {
    // 是路由
    if (menu.isRouter) {
      // 获取一个路由配置
      const tmp: IRouter = {
        path: menu.path,
        name: menu.name,
        component: menu.component
      }
      // 判断是否有子路由
      if (menu.children) {
        const t = toRouter(menu.children)
        // 子路由不为空
        if (t.length > 0) {
          tmp.children = t
          // re.push(...t)
        }
      }
      // 加上一个路由配置
      re.push(tmp)
    }
  })
  // 返回路由
  return re
}